package com.LiL.Wayne.Activity.fragment;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.LiL.Wayne.Activity.R;
import com.LiL.Wayne.Activity.R.anim;
import com.LiL.Wayne.Activity.R.id;
import com.LiL.Wayne.Activity.R.layout;
import com.LiL.Wayne.Activity.activity.MusicPlayer;
import com.audamob.audasingers.model.Music;
import com.audamob.audasingers.model.PlayList;
import com.audamob.audasingers.tool.adapter.MusicAdapter;
import com.audamob.audasingers.tool.view.ImageResizerUtils;

public class SwipeyTabFragmentMusic extends Fragment {
	private SimpleDateFormat FormatterDuration = new SimpleDateFormat("mm:ss");
	static ArrayList<Music> MusicList;
	ArrayList<Music> AllLyricList;
	static EditText SearchEdit;

	static SwipeyTabFragmentMusic f;

	public static Fragment newInstance(String title) {
		f = new SwipeyTabFragmentMusic();
		Bundle args = new Bundle();
		args.putString("title", title);
		f.setArguments(args);

		return f;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		// TODO Auto-generated method stub
		super.onHiddenChanged(hidden);
	}

	static ListView ListVideo;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		ViewGroup root = (ViewGroup) inflater.inflate(
				R.layout.auda_layout_activity_music, null);
		ViewGroup mainContainer=(ViewGroup)root.findViewById(R.id.MainContainer);
		Typeface font =Typeface.createFromAsset(getActivity().getAssets(), "ExoMedium.otf");

		ImageResizerUtils.setFont(getActivity(), mainContainer, font);
		final String title = getArguments().getString("title");

		createList();
		AllLyricList = new ArrayList<Music>();
		for (int s = 0; s < MusicList.size(); s++)
			AllLyricList.add(MusicList.get(s));

		Log.e("Songs", "size = " + AllLyricList.size());
		ListVideo = (ListView) root.findViewById(R.id.ListAppM);
		ListVideo.setAdapter(new MusicAdapter(getActivity(), MusicList));
		SearchEdit = (EditText) root.findViewById(R.id.searchedit);

		ThreadUpdateList th = new ThreadUpdateList(handlerLoad);
		th.start();
		return root;

	}

	boolean ModePlaylist = false;
	PlayList pl = null;
	int plID = 0;
	ArrayList<PlayList> ArrayPlaylist;

	@Override
	public void onResume() {
		super.onResume();

	}
	public static ArrayList<PlayList> restorePlayListList() throws IOException,
			ClassNotFoundException {
		FileInputStream fin = new FileInputStream(f.getActivity().getCacheDir()
				.getAbsolutePath()
				+ "/playlistlist");
		ObjectInputStream in = new ObjectInputStream(fin);
		ArrayList<PlayList> object = (ArrayList<PlayList>) in.readObject();
		in.close();
		return object;
	}

	public static void sauvegardPlayListList(ArrayList<PlayList> s)
			throws IOException {
		FileOutputStream fout = new FileOutputStream(f.getActivity()
				.getCacheDir().getAbsolutePath()
				+ "/playlistlist");
		ObjectOutputStream out = new ObjectOutputStream(fout);

		out.writeObject(s);
		out.close();
	}

	final Handler handlerLoad = new Handler() {

		public void handleMessage(Message msg) {

			try {
				MusicList.clear();
				Log.e("Songs", "AllLyric size " + AllLyricList.size());
				for (int i = 0; i < AllLyricList.size(); i++) {

					if (AllLyricList
							.get(i)
							.getName()
							.toLowerCase()
							.contains(
									("" + SearchEdit.getText().toString())
											.toLowerCase())) {

						MusicList.add(AllLyricList.get(i));

					}
				}
				Log.e("Songs", "change :: " + MusicList.size());
				ListVideo = (ListView) getActivity()
						.findViewById(R.id.ListAppM);
				ListVideo.setAdapter(new MusicAdapter(getActivity(), MusicList));
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	};

	int EditSongNameTextlength = 0;
	int EditAlbumNameTextlength = 0;
	private static boolean[] HideImageImageselection;

	private class ThreadUpdateList extends Thread {
		Handler mHandler;
		boolean Done = false;

		ThreadUpdateList(Handler h) {
			mHandler = h;
		}

		public void run() {
			while (!Done) {
				try {
					currentThread().sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// Log.e("Songs","change : "+SearchEdit.getText().length());
				if (SearchEdit.getText().length() == EditSongNameTextlength) {
				} else {
					Log.e("Songs", "change");
					EditSongNameTextlength = SearchEdit.getText().length();

					Message msg = mHandler.obtainMessage();
					Bundle b = new Bundle();
					b.putString("url", "");
					msg.setData(b);
					mHandler.sendMessage(msg);
				}
			}
		}
	}

	public void createList() {

		MusicList = new ArrayList<Music>();
		MusicList.add(new Music("Mirror",
				"http://r-f.wen9.org/MP3s/new5/Lil-Wayne_mirror.mp3"));

		MusicList
				.add(new Music(
						"Drop the world",
						"http://www.hiphopstage.cz/archiv/audio/lil-wayne_drop-the-world-ft-eminem_hiphopstage.mp3"));
		MusicList
				.add(new Music(
						"She Will",
						"http://bigtimebeats.com/wp-content/uploads/She-Will-feat.-Drake-Rick-Ross-Lil-Wayne-BIGTIMEBEATS.com_.mp3"));
		MusicList.add(new Music("Lollipop",
				"http://www.partyben.com/PartyBen-LollipopRemix2.mp3"));
		MusicList
				.add(new Music(
						"6 Foot 7 Foot",
						"http://www.sinternet.com/ssp_director/album-audio/LilWayne_6Foot7Foot_Clean.mp3"));
		MusicList
				.add(new Music(
						"How to Love",
						"http://decedope.com/wp-content/uploads/2011/05/Lil-Wayne-How-To-Love-Decentlydope.mp3"));
		MusicList
				.add(new Music(
						"Right Above It",
						"http://trckr.hulkshare.com/hulkdl/itxn5f9amauz/lil_wayne_ft._drake_-_right_above_it_(instrumental).mp3?z=0"));
		MusicList.add(new Music("Throw It Up",
				"http://www.fullsongs.net/alt.php?url=31353&source=7&opt=0"));
		MusicList.add(new Music("Let It Rock",
				"http://thedockingstation.ca/audio/2008-10/Let_It_Rock.mp3"));
		MusicList
				.add(new Music(
						"Down",
						"http://jakesallstarkaraoke.com/media/Jay_Sean_ft_Lil_Wayne_-_Down.mp3",
						"Jay_Sean"));

		MusicList
				.add(new Music(
						"Duffle Bag Boy",
						"http://star.walagata.com/w/army271/Playaz_Circle_ft_Lil_Wayne_-_Duffle_Bag_Boy.mp3"));
		MusicList
				.add(new Music(
						"A Milli",
						"http://star.walagata.com/w/army271/Playaz_Circle_ft_Lil_Wayne_-_Duffle_Bag_Boy.mp3"));
		MusicList
				.add(new Music(
						"I Can Only Imagine",
						"http://everythinglooksbrighter.files.wordpress.com/2012/04/i-can-only-imagine-feat-chris-brown-lil-wayne-david-guetta-daddys-groove-remix.mp3",
						"David_Guetta"));

		MusicList.add(new Music("Your Smile",
				"http://www.fullsongs.net/alt.php?url=31306&source=7&opt=0",
				"Bobby_Valentino"));

		MusicList
				.add(new Music(
						"Lock U Down",
						"http://elarci.wen9.org/Mp3/mp3/Mya_ft._Lil_Wayne-Lock_You_Down_elarci.wen9.org_.mp3",
						"Mya"));

		MusicList
				.add(new Music(
						"Soldier",
						"http://mp3.music.lib.ru/mp3/f/folamix/destinys_child_feat_ti_lil_wayne_-_soldier-2.mp3"));

		MusicList
				.add(new Music(
						"Turn Off",
						"http://www.therundown.tv/audio/keri_hilson_ft_lil_wayne_-_turn_off_prod_by_polo_da_don.mp3"));
		MusicList
				.add(new Music("Fireman",
						"http://kyblockheadpits.com/media/02-lil_wayne-fireman__main_-butt.mp3"));

		MusicList
				.add(new Music(
						"Money To Blow",
						"http://www.xmix.com/audiosamples/U133/Birdman_ft_Drake_and_Lil_Wayne-Money_To_Blow-XMIX_Remix-XMIXU.mp3",
						"Drake"));
		MusicList
				.add(new Music(
						"Unstoppable",
						"http://www.billandchelle.com/fitness/music_samples/Unstoppable_kat_deluna_lil_wayne.mp3"));
		MusicList
				.add(new Music("Every Girl",
						"http://www.56fashion.com/jukebox/mp3/lil_wayne-every_girl.mp3"));
		MusicList
				.add(new Music(
						"Knockout",
						"http://blogxilla.com/blog3/wp-content/uploads/2009/12/Lil-Wayne-Knockout-_feat-Nicki-Minaj_.mp3"));
		MusicList
				.add(new Music(
						"Sweetest Girl",
						"http://djkimusic.com/music/WYCLEF_FT_AKON__LIL_WAYNE___RAEKWON_-_SWEETEST_GIRL__REMIX_.mp3",
						"Akon"));

		MusicList
				.add(new Music(
						"I Can Transform Ya",
						"http://www.djdivsa.com//uploads/blogs/Chris_Brown_Lil_Wayne_-_I_Can_Transform_Ya_Divsa_Remix.mp3",
						"Chris_Brown"));
		MusicList
				.add(new Music(
						"Respect My Conglomerate",
						"http://letanque.files.wordpress.com/2009/05/03-respect-my-conglomerate-feat-lil-wayne-jadakiss-prod-by-focus-dr-dre.mp3",
						"Busta_Rhymes"));
		MusicList
				.add(new Music(
						"Y.U. Mad",
						"http://bigtimebeats.com/wp-content/uploads/Y.U.-Mad-feat.-Lil-Wayne-Nicki-Minaj-Birdman-BIGTIMEBEATS.com_.mp3",
						"Birdman"));
		MusicList
				.add(new Music(
						"Look At me Now",
						"http://www.officialheavyhitters.com/uploads/Chris_Brown__Busta_Rhymes_and_Lil_Wayne_-_Look_At_Me_Now__TRaXStars_Uptempo_Mix__FINAL_.mp3",
						"Chris_Brown"));
		MusicList
				.add(new Music(
						"Single Again",
						"http://www.therundown.tv/audio/trina-single_again_remix_feat_rick_ross_plies_lil_wayne.mp3"));

		MusicList
				.add(new Music(
						"Girls Around The World",
						"http://euraznlao.free.fr/zik/03-lloyd-girls_all_around_the_world_ft._lil_wayne.mp3",
						"Lloyd"));
		MusicList
				.add(new Music(
						"Mrs. Officer",
						"http://promodj.com/download/1284764/Lil_Wayne_feat_Bobby_Valentino_Mrs_Officer.mp3"));
		MusicList
				.add(new Music(
						"HYFR (Hell Ya Fucking Right)",
						"http://www.carlthatruth.com/wp-content/uploads/2011/11/Drake-feat.-Lil-Wayne-HYFR-Hell-Ya-Fuckin-Right.mp3",
						"Drake"));
		MusicList.add(new Music("My Life",
				"http://www.fullsongs.net/alt.php?url=31352&source=7&opt=0"));

		MusicList
				.add(new Music(
						"I am so paid",
						"http://www.beatsandbombs.com/wp-content/uploads/2008/10/akon-im_so_paid_feat_lil_wayne.mp3",
						"Akon"));
		MusicList
				.add(new Music(
						"So Good",
						"http://defpenradio.com/wp-content/2011/11/shanell-drake-lil-wayne-so-good-IFWT.mp3"));

		MusicList
				.add(new Music(
						"My Homies Still",
						"http://www.panamaready.net/download/Lil_Wayne_Feat._Big_Sean_-_My_Homies_Still.mp3"));

		MusicList
				.add(new Music(
						"Dirty Dancer",
						"http://promodj.com/download/3220431/Enrique_Iglesias_ft_Usher_Lil_Wayne_Dirty_Dancer_Dj_Dmitex_remix.mp3",
						"Enrique_Iglesias"));

		MusicList
				.add(new Music(
						"Revolver",
						"http://hiphopmusicdotcom.com/wp-content/uploads/2009/09/Madonna-ft.-Lil-Wayne-Revolver.mp3"));
		MusicList
				.add(new Music(
						"Enough Of No Love",
						"http://mp3.mp3pulse.ru/Keyshia_Cole_feat._Lil_Wayne_-_Enough_Of_No_Love_[mp3pulse.ru].mp3",
						"Keyshia_Cole"));

		MusicList
				.add(new Music("YM Salute",
						"http://wwsmag.com/updates/wp-content/uploads/Lil-Wayne-Young-Money-Salute.mp3"));

		MusicList.add(new Music("Ball",
				"http://dl.musicmegabox.net/work/files/731/I_BALL.mp3"));

		MusicList
				.add(new Music(
						"Sweat",
						"http://www.woo55.com/adata/9206/04.%20Bow%20Wow%20Ft.%20Lil%20Wayne%20-%20Sweat%20(www.SongsLover.com).mp3",
						"Bow_Bow"));
		MusicList
				.add(new Music(
						"Stuttin Like My Daddy",
						"http://dl.musicmegabox.net/work/files2/321/Stuntin_like_my_daddy.mp3",
						"Birdman"));
		MusicList
				.add(new Music(
						"John",
						"http://umrg.edgeboss.net/download/umrg/433-lilwayne/lil_wayne_john_rickross.mp3"));
	}

	public static Boolean State_Edit = false;

	public static void Mode_Edit_On() {

		SearchEdit.setEnabled(false);
		HideImageImageselection = new boolean[MusicList.size()];

		AppAdapterEdit ad = f.new AppAdapterEdit();

		ListVideo.setAdapter(ad);
		State_Edit = true;
	}

	public class AppAdapterEdit extends BaseAdapter {
		private LayoutInflater mInflater;

		public AppAdapterEdit() {
			mInflater = (LayoutInflater) getActivity().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return MusicList.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolderImage_edit holder;
			if (convertView == null) {
				holder = new ViewHolderImage_edit();

				convertView = mInflater.inflate(R.layout.auda_item_music_edit, null);
				holder.checkbox = (CheckBox) convertView
						.findViewById(R.id.checkbox);

				holder.Name = (TextView) convertView.findViewById(R.id.nam);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolderImage_edit) convertView.getTag();
			}
			holder.checkbox.setId(position);

			holder.Name.setId(position);

			holder.checkbox.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					CheckBox cb = (CheckBox) v;
					int id = cb.getId();
					if (HideImageImageselection[id]) {
						cb.setChecked(false);
						HideImageImageselection[id] = false;
					} else {
						cb.setChecked(true);
						HideImageImageselection[id] = true;
					}
				}
			});

			holder.checkbox.setChecked(HideImageImageselection[position]);
			holder.Name.setText(MusicList.get(position).getName());

			holder.id = position;

			return convertView;
		}
	}

	class ViewHolderImage_edit {
		CheckBox checkbox;

		TextView Name;

		int id;
	}

	public static void Mode_Edit_Of() {

		SearchEdit.setEnabled(true);
		 MusicAdapter ad = new  MusicAdapter(f.getActivity(), MusicList);
		ListVideo.setAdapter(ad);
		State_Edit = false;
		ArrayList<PlayList> list = new ArrayList<PlayList>();
		try {
			list = restorePlayListList();
		} catch (Exception e) {
			// TODO: handle exception
		}
		PlayList playList = new PlayList();
		Boolean found = false;
		for (int s = 0; s < list.size() && !found; s++) {
			PlayList pl = list.get(s);
			if (pl.getName().equalsIgnoreCase(
					MainContainerActivity.PlaylistName)) {
				playList = pl;
				found = true;
			}
		}

		ArrayList<Music> tempM = new ArrayList<Music>();

		for (int j = 0; j < HideImageImageselection.length; j++) {
			if (HideImageImageselection[j]) {
				tempM.add(MusicList.get(j));
			}
		}
		if (found) {
			playList.getListMusic().addAll(tempM);
		} else {
			playList = new PlayList(MainContainerActivity.PlaylistName, tempM);
		}

		try {
			sauvegardPlayListList(list);
		} catch (Exception e) {
			// TODO: handle exception
		}

		SwipeyTabFragmentMusicPlayList.update_your_sel();

	}

}